﻿using CloudMusic.Helper;
using CloudMusic.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace CloudMusic.ViewModel
{
    public class PageDaySongsViewModel : MusicViewModelBase
    {

  
        private Visibility subVIS;
        private ObservableCollection<MusicModel> daySongs;
        public PageDaySongsViewModel()
        {
            Weeks = DateTimeHelper.GetWeek();
            Days = DateTimeHelper.GetDays();
            GetDaySongsList();
        }
        /// <summary>
        /// 周几
        /// </summary>
        private string week;
        public string Weeks
        {
            get { return week; }
            set { week = value; OnPropertyChanged("Weeks"); }
        }
        /// <summary>
        /// 日期
        /// </summary>
        private string day;
        public string Days
        {
            get { return day; }
            set { day = value; OnPropertyChanged("Days"); }
        }
        /// <summary>
        /// 每日歌单
        /// </summary>
        public ObservableCollection<MusicModel> DaySongs
        {
            get
            {
                if (daySongs == null)
                {
                    daySongs = new ObservableCollection<MusicModel>();
                }
                return daySongs;
            }
            set { daySongs = value;OnPropertyChanged("DaySongs"); }
        }
        /// <summary>
        /// 数据处理
        /// </summary>
        public void GetDaySongsList()
        {
            try
            {
                Task.Run(async () =>
                {

                    await Task.Run(() =>
                    {
                        var cookie = "MUSIC_U=56418e440a522fbe7e0c533c21f2736679df3d2d50cdbf020bd4412c8f90af58e0235a7f03fa9bf1; Expires=Tue, 08-Sep-2020 03:01:41 GMT; Path=/;__remember_me=true; Expires=Tue, 08-Sep-2020 03:01:41 GMT; Path=/;__csrf=f34c9ce44fb09a491dd90874224ed935; Expires=Tue, 08-Sep-2020 03:01:51 GMT; Path=/";
                        var json = CommonHelper.GetEquipList<DaySongsModel>(1, "http://localhost:3000/recommend/songs?cookie="+cookie);
                        List<DailySongsItem> dailySong = json.data.dailySongs;
                        for (int i = 0; i < dailySong.Count; i++)
                        {
                            MusicModel mm = new MusicModel
                            {
                                Index = (i + 1).ToString(),
                                Album = dailySong[i].al.name,
                                AlbumId = dailySong[i].al.id,
                                Name = dailySong[i].name,
                                Subtitle = GetSubtitle(dailySong[i].alia),
                                Id = dailySong[i].id,
                                CoverTitle = CommonHelper.GetTitle(dailySong[i].name, dailySong[i].alia),
                                Artist = dailySong[i].ar[0].name,
                                SingerId = dailySong[i].ar[0].id,
                                Type = 1,
                                PicUrl = dailySong[i].al.picUrl,
                                SongId = 0,
                                CanPlay = true,
                                TimeStr = DateTimeHelper.GetTime((dailySong[i].dt / 1000).ToString()),
                                MVVis = CommonHelper.GetMvVis(dailySong[i].mv),
                                VIPVis = CommonHelper.GetVis(dailySong[i].privilege.fee),
                                STVis = CommonHelper.GetVis(dailySong[i].privilege.fee),
                                SQVis = CommonHelper.GetVis(dailySong[i].privilege.maxbr),
                                Source = "每日歌单",
                                Pop = dailySong[i].pop.ToString(),
                                SubtitleVis = subVIS,
                                Playing = false,
                                HighQuality = 4,
                                Like = CommonHelper.CheckLikes(dailySong[i].id)
                            };
                            ThreadPool.QueueUserWorkItem(delegate
                            {
                                SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current.Dispatcher));
                                SynchronizationContext.Current.Post(pl =>
                                {
                                    DaySongs.Add(mm);
                                }, null);
                            });
                        }
                    });
                });
            }
            catch (System.Exception) { }
        }
        /// <summary>
        /// 副标题操作
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string GetSubtitle(List<string> str)
        {
            string al = null;
            subVIS = Visibility.Collapsed;
            if (str.Count > 0)
            {
                for (int i = 0; i < str.Count; i++)
                {
                    al += "(" + str[i] + ")";
                }
                subVIS = Visibility.Visible;
            }
            return al;
        }
    }
}
